﻿/*
 *	(*^。^*) TFrameWork License (*^。^*)
 */

/*
 *  Title :
 *
 *  Description :
 *
 *
 *  Date :
 *
 *  Author : DengPing
 *
 *  Modify :
 */

using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace TFrameWork.Module.ActionEditor
{
    public class PrefabInspector : RecordEventBaseInspector
    {
        private Label particleLenth;

        private PrefabEvent _prefabData;

        public override void Show(VisualElement root)
        {
            this.root = root;
            itemStyle = ActionEditorInspector.currentTrackItemStyle;
            data = itemStyle.eventData;
            _prefabData = data as PrefabEvent;

            ShowRecord();
            particleLenth = new Label("预制体原始播放帧数");
            base.root.Add(particleLenth);
            ShowTrackName();
            ShowAudioClip();
            ShowStartIndex();
            ShowDurationIndex();
            ShowPosition();
            ShowRotate();
            ShowScale();
            ShowAutoDestroy();
            //ShowApplyModelTransformButton();

            ShowAnimationInfo();

        }

        private void ShowAudioClip()
        {
            ObjectField animationClipAssetField = new ObjectField("预制体资源");
            animationClipAssetField.objectType = typeof(GameObject);
            animationClipAssetField.value = _prefabData.prefab;
            animationClipAssetField.RegisterValueChangedCallback(p =>
            {
                if (p.newValue == null) return;
                GameObject clip = p.newValue as GameObject;

                // 保存到配置
                _prefabData.prefab = clip;
                _prefabData.prefabPath = AssetDatabase.GetAssetPath(clip);
                // 修改自身显示效果
                particleLenth.text = "预制体原始播放帧数:      " + _prefabData.GetDefaultDuration();
                _prefabData.durationIndex = _prefabData.GetDefaultDuration();
                itemStyle.ResetView();
            });
            root.Add(animationClipAssetField);
        }

        private void ShowAnimationInfo()
        {
            if (_prefabData.prefab != null)
            {
                particleLenth.text = "预制体原始播放帧数:      " + _prefabData.GetDefaultDuration();
            }
        }

        private void ShowPosition()
        {
            Vector3Field positon = new Vector3Field("位置");
            positon.value = _prefabData.position;
            positon.RegisterValueChangedCallback(p =>
            {
                if (_prefabData.position != positon.value)
                {
                    _prefabData.position = positon.value;
                    itemStyle.ResetView();
                }
            });
            root.Add(positon);
        }

        private void ShowRotate()
        {
            Vector3Field retate = new Vector3Field("旋转");
            retate.value = _prefabData.rotate;
            retate.RegisterValueChangedCallback(p =>
            {
                if (_prefabData.rotate != retate.value)
                {
                    _prefabData.rotate = retate.value;
                    itemStyle.ResetView();
                }
            });
            root.Add(retate);
        }

        private void ShowScale()
        {
            Vector3Field retate = new Vector3Field("缩放");
            retate.value = _prefabData.scale;
            retate.RegisterValueChangedCallback(p =>
            {
                if (_prefabData.scale != retate.value)
                {
                    _prefabData.scale = retate.value;
                    itemStyle.ResetView();
                }
            });
            root.Add(retate);
        }

        private void ShowAutoDestroy()
        {
            Toggle auto = new Toggle("自动销毁");
            auto.value = _prefabData.autoDestroy;
            auto.RegisterValueChangedCallback(p =>
            {
                if (_prefabData.autoDestroy != auto.value)
                {
                    _prefabData.autoDestroy = auto.value;
                    itemStyle.ResetView();
                }
            });
            root.Add(auto);
        }
    }
}
